package com.bxait.toymail.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @author Chen
 * @description:  网关的配置类
 * @ClassName ToyMallCorsConfiguration
 * @create 2023-03-11 16:38
 * @Version 1.0
 */
@Configuration
public class ToyMallCorsConfiguration {

    /**
     * 方法说明：添加拦截器,主要为了解决请求跨域问题
     * @MethodName
     * @Description
     * @Author Chen
     * @EMAIL <a href="2277411582@qq.com"></a>
     * @Date 2023/3/11 16:41
     */
    @Bean
    public CorsWebFilter corsWebFilter(){
        // 基于url跨域，选择reactive包下的
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        // 配置跨域信息
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 允许跨域的头
        corsConfiguration.addAllowedHeader("*");
        // 允许跨域的请求
        corsConfiguration.addAllowedMethod("*");
        // 允许跨域的请求来源
        corsConfiguration.addAllowedOrigin("*");
        // 是否允许携带cookie跨域
        corsConfiguration.setAllowCredentials(true);

        //
        source.registerCorsConfiguration("/**",corsConfiguration);

        return new CorsWebFilter(source);
    }

}
